/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment.home;

import static com.dataoke.shoppingguide.app773026.bean.Constants.MEITUAN_RED_PACKAGE_URL;
import static com.dataoke.shoppingguide.app773026.bean.Constants.MEITUAN_SHOPPING_URL;
import static com.dataoke.shoppingguide.app773026.bean.Constants.MEITUAN_SUPERMARKET_URL;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.activity.webview.BrowserActivity;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuIndexNavBean;
import com.dataoke.shoppingguide.app773026.util.AppUtil;
import com.dataoke.shoppingguide.app773026.util.GlideUtils;
import com.dataoke.shoppingguide.app773026.util.HaoDanKuUtil;
import com.dataoke.shoppingguide.app773026.waimai.ImageActivity;
import com.dataoke.shoppingguide.app773026.waimai.MeituanShangchaoActivity;

import java.util.List;

/**
 * 频道适配器
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class NavAdapter extends BaseAdapter{
    private final Context mContext;
    private final List<HaodankuIndexNavBean> datas;

    public NavAdapter(Context mContext, List<HaodankuIndexNavBean> channel_info) {
        this.mContext = mContext;
        this.datas = channel_info;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = View.inflate(mContext, R.layout.item_nav,null);
            viewHolder = new ViewHolder();
            viewHolder.mIvNav = convertView.findViewById(R.id.iv_nav);
            viewHolder.mTvNav = convertView.findViewById(R.id.tv_nav);
            viewHolder.mTvCorNer = convertView.findViewById(R.id.tv_corner);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //根据位置得到对应的数据
        HaodankuIndexNavBean channelInfoBean = datas.get(position);
        if(position == 0 || position == 2) {
            viewHolder.mIvNav.setBackgroundResource(R.mipmap.mtnav);
        } else {
            //根据Glide设置图片
            GlideUtils.loadNavCrop(mContext, channelInfoBean.getImg(), viewHolder.mIvNav);
        }

        String title = channelInfoBean.getTitle();
        viewHolder.mTvNav.setText(title);
        initListener(convertView, position);
        String corner = HaoDanKuUtil.getCorner(title);
        if(!TextUtils.isEmpty(corner)) {
            viewHolder.mTvCorNer.setText(corner);
            viewHolder.mTvCorNer.setVisibility(View.VISIBLE);
        }

        return convertView;
    }
    static class ViewHolder{
        ImageView mIvNav;
        TextView mTvNav;
        TextView mTvCorNer;
    }

    protected void initListener(View view, int position) {
        view.setOnClickListener(v -> {
            if(position == 0) {
                AppUtil.openWxMeituan(mContext, MEITUAN_RED_PACKAGE_URL);
            } else if(position == 2) {
                AppUtil.openWxMeituan(mContext, MEITUAN_SUPERMARKET_URL);
            } else if(position == 3) {
                AppUtil.openWxBz(mContext);
            } else {
                HaodankuIndexNavBean bean = datas.get(position);
                BrowserActivity.show(mContext, HaoDanKuUtil.getUrl(bean), bean.getTitle());
            }
        });
    }
}
